package com.wangjw.leetcode;

import com.wangjw.dataStructure.segmentTree.SegmentTree;

/**
 * Created by wangjw on 18/7/4.
 */
public class LeetCode307_2 {

    private SegmentTree<Integer> segmentTree;

    public LeetCode307_2(int[] nums) {
        if (nums.length > 0) {
            Integer[] datas = new Integer[nums.length];
            for (int i = 0; i < nums.length; i++) {
                datas[i] = nums[i];
            }
            segmentTree = new SegmentTree<>(datas, (a, b) -> a + b);
        }
    }

    public void update(int index, int val) {
        if (segmentTree == null) {
            throw new IllegalArgumentException("Segment Tree is null");
        }
        segmentTree.set(index, val);
    }

    public int sumRange(int i, int j) {
        if (segmentTree == null) {
            throw new IllegalArgumentException("Segment Tree is null");
        }
        return segmentTree.query(i, j);
    }
}
